local skel = fk.CreateSkill {
  name = "emo__mingyun",
}

skel:addEffect(fk.DamageInflicted, {
  anim_type = "control",
  times = function (self, player)
    return 1 - player:usedEffectTimes(self.name, Player.HistoryTurn)
  end,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedEffectTimes(self.name, Player.HistoryTurn) == 0 then
      local targets = {}
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from and not move.from:isNude() and table.find(move.moveInfo, function(info)
            return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
          end) then
            table.insertIfNeed(targets, move.from)
          end
        end
        return false
      end, Player.HistoryTurn)
      if #targets > 0 then
        event:setCostData(self, {targets = targets})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local targets = event:getCostData(self).targets
    local tos = player.room:askToChoosePlayers(player, {
      max_num = 1, min_num = 1, skill_name = skel.name, targets = targets, cancelable = true,
      prompt = "#emo__mingyun-choose"
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]---@type ServerPlayer
    if to:isNude() then return end
    local cid = room:askToChooseCard(player, {
      target = to, skill_name = skel.name, flag = "he"
    })
    room:moveCardTo(cid, Card.DrawPile, nil, fk.ReasonPut, skel.name, nil, false, player)
    if not to.dead then
      to:drawCards(1, skel.name, "bottom")
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason ~= fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              if player.room:getCardArea(info.cardId) == Card.DiscardPile then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end
      if #ids > 0 then
        event:setCostData(self, {cards = ids})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local ids = event:getCostData(self).cards
    local cards = player.room:askToCards(player, {
      max_num = 1, min_num = 1, skill_name = skel.name, cancelable = true, expand_pile = ids,
      pattern = tostring(Exppattern{ id = ids }), prompt = "#emo__mingyun-put",
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCards({
      ids = event:getCostData(self).cards,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonPut,
      skillName = skel.name,
      proposer = player,
      drawPilePosition = -1,
    })
  end,
})


Fk:loadTranslationTable{
  ["emo__mingyun"] = "明蕴",
  [":emo__mingyun"] = "当你的手牌不因使用而进入弃牌堆后，你可以将其中一张置于牌堆底；每回合限一次，当有角色受到伤害时，你可以将本回合失去过牌的角色一张牌置于牌堆顶并令其从牌堆底摸一张牌。",
  ["#emo__mingyun-choose"] = "明蕴：你可以将本回合失去过牌的角色一张牌置于牌堆顶，令其从牌堆底摸一张牌",
  ["#emo__mingyun-put"] = "明蕴：你可以你失去的一张牌置于牌堆底",
}


return skel
